package so.shanku.essential.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/8.
 */
public class UserDataActivity extends BaseUIActivity {
	/**
	 * 女
	 */
	@ViewInject(id = R.id.tv_user_woman)
	private TextView tv_user_woman;
	/**
	 * 女
	 */
	@ViewInject(id = R.id.iv_woman, click = "iv_woman_click")
	private ImageView iv_woman;
	/**
	 * 男
	 */
	@ViewInject(id = R.id.tv_user_man)
	private TextView tv_user_man;
	/**
	 * 男
	 */
	@ViewInject(id = R.id.iv_man, click = "iv_man_click")
	private ImageView iv_man;
	/**
	 * 真实姓名
	 */
	@ViewInject(id = R.id.et_user_truename)
	private EditText et_user_truename;
	/**
	 * 昵称
	 */
	@ViewInject(id = R.id.et_user_nickname)
	private EditText et_user_nickname;
	/**
	 * 手机号
	 */
	@ViewInject(id = R.id.et_user_account)
	private EditText et_user_account;
	/**
	 * 邮箱
	 */
	@ViewInject(id = R.id.et_user_email)
	private EditText et_user_email;
	/**
	 * 确定
	 */
	@ViewInject(id = R.id.bt_confirm, click = "bt_confirm_click")
	private TextView bt_confirm;
	private String phone;//手机号码
	private String nickName;//昵称
	private int sex = 2;//性别
	private String realName;//真实姓名
	private String email;//邮箱

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_data);
		setCenter_title(R.string.check_data);
		enterSetData();
	}

	public void enterSetData() {
		et_user_account.setText(Utils.getUserAccount(UserDataActivity.this));
		et_user_email.setText(Utils.getUserEmail(UserDataActivity.this));
		et_user_nickname.setText(Utils.getUserNickName(UserDataActivity.this));
		et_user_truename.setText(Utils.getUserRealName(UserDataActivity.this));
		if (Utils.getUserSex(UserDataActivity.this) == 0) {
			manClick();
		} else if (Utils.getUserSex(UserDataActivity.this) == 1) {
			womanClick();
		} else {
			sexSecret();
		}
	}

	public void bt_confirm_click(View view) {     //确定
		if (judgeChange()) {
			getData_ChangeUserData();
		}
	}

	public void iv_man_click(View view) {     //男
		manClick();
		sex = 0;
	}

	public void iv_woman_click(View view) {
		//女
		womanClick();
		sex = 1;
	}

	public void manClick() {        //点击男
		tv_user_woman.setTextColor(Color.GRAY);
		tv_user_man.setTextColor(Color.BLACK);
		iv_woman.setImageResource(R.drawable.unchecked);
		iv_man.setImageResource(R.drawable.check);

	}

	public void womanClick() {    //点击女
		tv_user_man.setTextColor(Color.GRAY);
		tv_user_woman.setTextColor(Color.BLACK);
		iv_man.setImageResource(R.drawable.unchecked);
		iv_woman.setImageResource(R.drawable.check);
	}

	public void sexSecret() {    //性别保密
		tv_user_man.setTextColor(Color.GRAY);
		tv_user_woman.setTextColor(Color.GRAY);
		iv_man.setImageResource(R.drawable.unchecked);
		iv_woman.setImageResource(R.drawable.unchecked);
	}

	public boolean judgeMobileNo() {
		phone = et_user_account.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			//手机号码为空的情况
			toast.showToast(R.string.register_name_null);
			return false;
		} else if (!StringUtil.isMobileNO(phone)) {
			toast.showToast(R.string.phone_formal_error);
			return false;
		}
		return true;
	}


	public boolean judgeEmail() {
		boolean isjudge = true;
		email = et_user_email.getText().toString();
		if (TextUtils.isEmpty(email)) {
			//邮箱为空的情况
//            toast.showToast(R.string.email_is_empty);
//            isjudge = false;
		} else if (!StringUtil.isEmail(email)) {
			toast.showToast(R.string.email_style_error);
			isjudge = false;
		}
		return isjudge;
	}

	public boolean judgeChange() {
		nickName = et_user_nickname.getText().toString().trim();
		realName = et_user_truename.getText().toString().trim();
	   /* if (TextUtils.isEmpty(nickName)) {
	        toast.showToast(R.string.nickname_null);
            return false;
        } else */
		if (!TextUtils.isEmpty(nickName) && nickName.equalsIgnoreCase("null") || nickName.equalsIgnoreCase("nil") || nickName.equalsIgnoreCase("''") || nickName.equalsIgnoreCase("'") || nickName.equalsIgnoreCase("\"") || nickName.equalsIgnoreCase("\"\"")) {
			toast.showToast(R.string.nickname_invalidata);
			return false;
		}
//        if (TextUtils.isEmpty(realName)) {
//            toast.showToast(R.string.truename_null);
//            isRealName = false;
//        }
		return judgeEmail();
	}

	/**
	 * 修改用户数据
	 */
	private void getData_ChangeUserData() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.User_Key);
		params.put("NickName", nickName);
		params.put("sex", sex);
		params.put("email", email);
		params.put("userName", et_user_account.getText().toString());
		params.put("realName", realName);
		params.put("birthday", "");
		params.put("identityCardNo", "");
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_UpdateUserInfo);
		queue.setWhat(GetDataConfing.what_UpdateUserInfo);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}

	/**
	 * 数据回调
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(UserDataActivity.this,
						entity.getInfo())) {
					if (entity.getWhat() == GetDataConfing.what_UpdateUserInfo) {
						Utils.saveUserEmail(UserDataActivity.this, email);
						Utils.saveUserNickName(UserDataActivity.this, nickName);
						Utils.saveUserSex(UserDataActivity.this, sex);
						Utils.saveUserRealName(UserDataActivity.this, realName);
						toast.showToast(R.string.change_success);
						setResult(RESULT_OK);
						UserDataActivity.this.finish();

					}
				}
			} else {
				ShowGetDataError.showNetError(UserDataActivity.this);
			}
			loadingToast.dismiss();
		}
	};
}
